import {formatDate, triggerDelay} from "@/untils/untils";
import {workerHttpRequest} from "@/untils/workerHttpRequest";

self.onmessage = async (e) => {
    let data = e.data
    let result = {
        type: data,
        data: ''
    }
    if (data === 'async') {
        await workerHttpRequest("get", "/test", null, null, "proxy").then(res => {
            triggerDelay()
            // 将结果发送回主线程
            result.data = JSON.stringify(res.data)
            // 子线程处理数据并返回
            self.postMessage(result)
        })
    } else {
        result.data = '数据：' + e.data + '，<br>子线程处理时间：' + formatDate(new Date())
        // 子线程处理数据并返回
        self.postMessage(result)
    }
}